package com.xbongbong.paas.help.subform;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.help.subform.abstracts.AbstractUserAndDepTeamTransferSubForm;
import com.xbongbong.saas.domain.entity.ProductUserEntity;
import com.xbongbong.saas.domain.entity.UserAndDepTeamEntity;
import com.xbongbong.saas.domain.entity.competitor.CompetitorUserEntity;
import com.xbongbong.saas.enums.subform.CompetitorTeamEnum;
import com.xbongbong.saas.enums.subform.ProductTeamEnum;
import org.springframework.stereotype.Component;

/**
 * @author gaojie
 * @version v1.0
 * @date 2022/8/11 15:34
 * @since v1.0
 */
@Component
public class CompetitorTeamTransferSubForm extends AbstractUserAndDepTeamTransferSubForm {

    @Override
    public PaasFormDataEntity transferTeamToFormData(UserAndDepTeamEntity userAndDepTeamEntity) {
        CompetitorUserEntity competitorUserEntity = (CompetitorUserEntity) userAndDepTeamEntity;
        PaasFormDataEntity paasFormDataEntity = createPaasFormDataEntityForUserTeam(competitorUserEntity);

        JSONObject teamJson = JSONObject.parseObject(JSON.toJSONString(competitorUserEntity));

        JSONObject data = new JSONObject();

        CompetitorTeamEnum[] competitorTeamEnums = CompetitorTeamEnum.values();
        for (CompetitorTeamEnum competitorTeamEnum : competitorTeamEnums) {
            String dbAttr = competitorTeamEnum.getDbAttr();
            String dataAttr = competitorTeamEnum.getDataAttr();
            Integer fieldType = competitorTeamEnum.getFieldType();
            formatDataForEs(teamJson,data,dbAttr,dataAttr,fieldType);
        }
        paasFormDataEntity.setData(data);

        return paasFormDataEntity;
    }

    @Override
    public UserAndDepTeamEntity transferFormDataToTeam(PaasFormDataEntity paasFormDataEntity) {
        JSONObject data = paasFormDataEntity.getData();

        JSONObject dbData = new JSONObject();

        CompetitorTeamEnum[] competitorTeamEnums = CompetitorTeamEnum.values();
        for (CompetitorTeamEnum competitorTeamEnum : competitorTeamEnums) {
            String dbAttr = competitorTeamEnum.getDbAttr();
            String dataAttr = competitorTeamEnum.getDataAttr();
            Integer fieldType = competitorTeamEnum.getFieldType();
            formatDataForDb(data, dbData, dbAttr, dataAttr, fieldType);
        }
        CompetitorUserEntity competitorUser = JSON.parseObject(dbData.toJSONString(), CompetitorUserEntity.class);
        competitorUser.setAddTime(paasFormDataEntity.getAddTime());
        competitorUser.setUpdateTime(paasFormDataEntity.getUpdateTime());
        competitorUser.setCorpid(paasFormDataEntity.getCorpid());
        competitorUser.setDel(paasFormDataEntity.getDel());
        competitorUser.setId(paasFormDataEntity.getId());

        return competitorUser;
    }
}
